package tasks

import (
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/toolbox"
	"github.com/gogf/gf/util/gconv"
	"mdblog/models/blog"
)

func init() {
	t1 := toolbox.NewTask("CollectArticleClassifyNum", "0 */2 * * * *", CollectClassifyArticleNumTask)
	toolbox.AddTask("CollectArticleClassifyNum", t1)
}

func CollectClassifyArticleNumTask() error {
	var result []orm.Params
	_, err := orm.NewOrm().Raw("select classify, count(*) as total from blog_articles group by classify").Values(&result)
	if err != nil {
		return err
	}
	for _, p := range result {
		blog.SetClassifyArticleNum(gconv.Int(p["classify"]), gconv.Int(p["total"]))
	}
	return nil
}
